/*
                  InterfaceOptions Class Implementation
*/
#include "InterfaceOptions.h"

InterfaceOptions::InterfaceOptions()
{

}

InterfaceOptions::~InterfaceOptions()
{

}

int InterfaceOptions::Initialise()
{
    // Create the resolutions
    vector<string> Resolutions;
    Resolutions.push_back("1024x768");
    Resolutions.push_back("1280x800");
    Resolutions.push_back("1440x900");
    Resolutions.push_back("1680x1050");
    Resolutions.push_back("1920x1080");
    //*************************

    frmOptionTabs = new Frame;
    frmOptionTabs->Initialise(Point(50, 100), 1, 1);
    frmOptionTabs->setBackground("data//interface//options//optiontabsbg.png");
    frmOptionTabs->setVisible(true);
    frmOptionTabs->setFixed(true);

    btnGeneral = new Button;
    btnGeneral->Initialise(Point(50, 50), "General", "data//fonts//bold.ttf", 14, "data//interface//button.png", "data//interface//button_c.png", GetSDLColor(255, 255, 255));

    btnVideo = new Button;
    btnVideo->Initialise(Point(50, 100), "Video", "data//fonts//bold.ttf", 14, "data//interface//button.png", "data//interface//button_c.png", GetSDLColor(255, 255, 255));

    btnAudio = new Button;
    btnAudio->Initialise(Point(50, 150), "Audio", "data//fonts//bold.ttf", 14, "data//interface//button.png", "data//interface//button_c.png", GetSDLColor(255, 255, 255));

    btnControls = new Button;
    btnControls->Initialise(Point(50, 200), "Controls", "data//fonts//bold.ttf", 14, "data//interface//button.png", "data//interface//button_c.png", GetSDLColor(255, 255, 255));

    btnNetwork = new Button;
    btnNetwork->Initialise(Point(50, 250), "Network", "data//fonts//bold.ttf", 14, "data//interface//button.png", "data//interface//button_c.png", GetSDLColor(255, 255, 255));

    frmOptionTabs->AddWidget(btnGeneral, "btnGeneral");
    frmOptionTabs->AddWidget(btnVideo, "btnVideo");
    frmOptionTabs->AddWidget(btnAudio, "btnAudio");
    frmOptionTabs->AddWidget(btnControls, "btnControls");
    frmOptionTabs->AddWidget(btnNetwork, "btnNetwork");

    frmOptions = new Frame;
    frmOptions->Initialise(Point(0, 0), 1, 1);
    frmOptions->setBackground("data//interface//options//optionframe.png");
    frmOptions->setVisible(true);
    frmOptions->setFixed(true);
    frmOptions->WindowAlign(ALIGN_RIGHT, GetWindowSize());
    frmOptions->MoveRelative(-50, 100);

    lblResolution = new Label;
    lblResolution->Initialise(Point(50, 25), "Data//Fonts//gui.ttf", 24, "Resolution: ", GetSDLColor(255, 255, 255));

    lstResolution = new Listbox;
    lstResolution->Initialise(Point(250, 25), 24, "Data//Fonts//gui.ttf", 100, 0xFFFFFF, GetSDLColor(0, 0, 0));
    lstResolution->SetListItems(Resolutions);
    lstResolution->setSelectedItem(lexical_cast<string>(ConfigFile::iCurrentWindowResX) + "x" + lexical_cast<string>(ConfigFile::iCurrentWindowResY));

    chkFullscreen = new Checkbox;
    chkFullscreen->Initialise(Point(250, 75), "Data//Fonts//gui.ttf", 24, "data//interface//check.png", "data//interface//uncheck.png", "Fullscreen?", GetSDLColor(255, 255, 255));
    chkFullscreen->setChecked(ConfigFile::bFullscreen);

    lblMetaserver = new Label;
    lblMetaserver->Initialise(Point(50, 25), "Data//Fonts//gui.ttf", 24, "Metaserver: ", GetSDLColor(255, 255, 255));

    txtMetaserver = new TextBox;
    txtMetaserver->Initialise(Point(200, 30), "Data//Fonts//gui.ttf", 18, 2, "data//interface//textbox.png", GetSDLColor(255, 255, 255), 500);
    txtMetaserver->setText(ConfigFile::strMetaServer);

    lblPort = new Label;
    lblPort->Initialise(Point(50, 75), "Data//Fonts//gui.ttf", 24, "Port: ", GetSDLColor(255, 255, 255));

    txtPort = new TextBox;
    txtPort->Initialise(Point(200, 80), "Data//Fonts//gui.ttf", 18, 2, "data//interface//textbox.png", GetSDLColor(255, 255, 255), 5);
    txtPort->setText(lexical_cast<string>(ConfigFile::iPort));

    frmOptions->AddWidget(chkFullscreen, "chkFullscreen");
    frmOptions->AddWidget(lblResolution, "lblResolution");
    frmOptions->AddWidget(lstResolution, "lstResolution");
    frmOptions->AddWidget(lblMetaserver, "lblMetaserver");
    frmOptions->AddWidget(txtMetaserver, "txtMetaserver");
    frmOptions->AddWidget(lblPort, "lblPort");
    frmOptions->AddWidget(txtPort, "txtPort");

    frmOptionbar = new Frame;
    frmOptionbar->Initialise(Point(0, 0), ConfigFile::iCurrentWindowResX, 100);
    frmOptionbar->setVisible(true);
    frmOptionbar->setFixed(true);
    frmOptionbar->WindowAlign(ALIGN_LEFT, GetWindowSize());
    frmOptionbar->WindowAlign(ALIGN_BOTTOM, GetWindowSize());

    btnBack = new Button;
    btnBack->Initialise(Point(50, 50), "Back to Menu", "data//fonts//bold.ttf", 14, "data//interface//button.png", "data//interface//button_c.png", GetSDLColor(255, 255, 255));

    btnSave = new Button;
    btnSave->Initialise(Point(ConfigFile::iCurrentWindowResX - 250, 50), "Save", "data//fonts//bold.ttf", 14, "data//interface//button.png", "data//interface//button_c.png", GetSDLColor(255, 255, 255));

    frmOptionbar->AddWidget(btnBack, "btnBack");
    frmOptionbar->AddWidget(btnSave, "btnSave");

    frmgOptions.AddFrame(frmOptionbar, "frmOptionbar");
    frmgOptions.AddFrame(frmOptionTabs, "frmOptionTabs");
    frmgOptions.AddFrame(frmOptions, "frmOptions");

    frmOptions->CreateTab("General");
    frmOptions->CreateTab("Video");
    frmOptions->CreateTab("Audio");
    frmOptions->CreateTab("Controls");
    frmOptions->CreateTab("Network");

    frmOptions->AddWidgetToTab("Video", chkFullscreen);
    frmOptions->AddWidgetToTab("Video", lblResolution);
    frmOptions->AddWidgetToTab("Video", lstResolution);
    frmOptions->AddWidgetToTab("Network", lblMetaserver);
    frmOptions->AddWidgetToTab("Network", txtMetaserver);
    frmOptions->AddWidgetToTab("Network", lblPort);
    frmOptions->AddWidgetToTab("Network", txtPort);

    // General is the default tab
    frmOptions->SetVisibleTab("General");

    imgBackground.Initialise(ConfigFile::strBGPath, Point(0, 0));
}

InterfaceType InterfaceOptions::HandleInput(const SDL_Event& event)
{
    frmgOptions.parseInput(event);

    return CheckWidgetEvents();
}

int InterfaceOptions::Draw(SDL_Surface *surface)
{
    imgBackground.BlitToSurface(surface);
    frmgOptions.BlitToSurface(surface);
}

InterfaceType InterfaceOptions::CheckWidgetEvents()
{
    GUIEvent guievent;

    while (frmOptionbar->PollEvent(guievent) == 0)
    {
        switch (guievent.getEventType())
        {
            case GUIEVENT_LEFTMOUSEUP:
            {
                if (guievent.getSource() == "btnBack")
                {
                    return MENU;
                }

                if (guievent.getSource() == "btnSave")
                {
                    SaveSettings();
                }
            }
            break;
        }
    }

    while (frmOptionTabs->PollEvent(guievent) == 0)
    {
        switch (guievent.getEventType())
        {
            case GUIEVENT_LEFTMOUSEUP:
            {
                if (guievent.getSource() == "btnGeneral")
                {
                    frmOptions->SetVisibleTab("General");
                }

                if (guievent.getSource() == "btnVideo")
                {
                    frmOptions->SetVisibleTab("Video");
                }

                if (guievent.getSource() == "btnAudio")
                {
                    frmOptions->SetVisibleTab("Audio");
                }

                if (guievent.getSource() == "btnControls")
                {
                    frmOptions->SetVisibleTab("Controls");
                }

                if (guievent.getSource() == "btnNetwork")
                {
                    frmOptions->SetVisibleTab("Network");
                }
            }
            break;
        }
    }

    return NONE;
}

void InterfaceOptions::SaveSettings()
{
    // Video********************************************************
    string strResolution = lstResolution->getSelectedItem();

    ConfigFile::iWindowResX = lexical_cast<int>(strResolution.substr(0, strResolution.find("x")));
    ConfigFile::iWindowResY = lexical_cast<int>(strResolution.substr(strResolution.find("x") + 1));

    ConfigFile::bFullscreen = chkFullscreen->getChecked();
    //**************************************************************

    // Network******************************************************
    ConfigFile::strMetaServer = txtMetaserver->getText();

    ConfigFile::iPort = lexical_cast<int>(txtPort->getText());
    //**************************************************************
}
